import time
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from common.Util import BlogDriver

# 博客详情页
class BlogDetail:
    driver = ""
    url = ""

    # 构造函数
    def __init__(self):
        self.driver = BlogDriver.driver
        # 一篇博客的url
        self.url = "http://8.137.19.140:9090/blog_detail.html?blogId=115833"
        self.driver.get(self.url)

    # 检查博客详情页的各种信息是否存在
    def BlogLoginDetailTest(self):
        # 检查一下博客的标题是否存在
        self.driver.find_element(By.CSS_SELECTOR, "body > div.container > div.right > div > div.title")
        # 检查一下发布博客的时间是否存在
        self.driver.find_element(By.CSS_SELECTOR, "body > div.container > div.right > div > div.date")
        # 检查一下博客的内容是否存在
        self.driver.find_element(By.CSS_SELECTOR, "#detail")
        # 检查一下用户基本信息的昵称是否存在
        self.driver.find_element(By.CSS_SELECTOR, "body > div.container > div.left > div > h3")
        # 检查一下用户基本信息的文章是否存在
        self.driver.find_element(By.CSS_SELECTOR,"body > div.container > div.left > div > div:nth-child(4) > span:nth-child(1)")
        # 检查一下用户基本信息的分类是否存在
        self.driver.find_element(By.CSS_SELECTOR,"body > div.container > div.left > div > div:nth-child(4) > span:nth-child(2)")
        # 检查一下编辑按钮是否存在
        self.driver.find_element(By.CSS_SELECTOR,"body > div.container > div.right > div > div.operating > button:nth-child(1)")
        # 检查一下删除按钮是否存在
        self.driver.find_element(By.CSS_SELECTOR,"body > div.container > div.right > div > div.operating > button:nth-child(2)")
        # 添加屏幕截图
        BlogDriver.GetScreeShot()
        time.sleep(2)

    # 检查删除博客，回到列表页
    def DeleteBlogLoginDetailTest(self):
        # 点击第一篇博客删除按钮
        BlogDriver.GetScreeShot()
        self.driver.find_element(By.CSS_SELECTOR,"body > div.container > div.right > div > div.operating > button:nth-child(2)").click()
        # 点击删除按钮后，会出现一个弹窗，等待2秒
        wait = WebDriverWait(self.driver, 2)
        # EC.alert_is_present() 仅检查弹窗是否存在
        wait.until(EC.alert_is_present())
        # 找到弹窗
        alert = self.driver.switch_to.alert
        # 判断一下弹窗里面的内容是否是确定删除？
        actual = alert.text
        print(actual)
        assert actual == "确定删除?"
        # 点击确定删除按钮
        alert.accept()
        time.sleep(1)
        BlogDriver.GetScreeShot()
        # 删除后判断一下是否返回到了博客列表页
        time.sleep(1)
        rettitle = self.driver.title
        retUrl = self.driver.current_url
        assert rettitle == "博客列表页"
        assert retUrl == "http://8.137.19.140:9090/blog_list.html"
        print("rettitle:" + rettitle)
        print("retUrl:" + retUrl)

    # 在未登录的状态下
    def NotBlogLoginDetailTest(self):
        # # 找到用户命名输入框和密码输入框把里面的内容清空
        # self.driver.find_element(By.CSS_SELECTOR, "#username").clear()
        # self.driver.find_element(By.CSS_SELECTOR, "#password").clear()
        self.driver.get("http://8.137.19.140:9090/blog_detail.html?blogId=115833")
        time.sleep(2)
        # 检查一下博客的标题是否存在
        self.driver.find_element(By.CSS_SELECTOR, "body > div.container > div.right > div > div.title")
        # 检查一下发布博客的时间是否存在
        self.driver.find_element(By.CSS_SELECTOR, "body > div.container > div.right > div > div.date")
        # 检查一下博客的内容是否存在
        self.driver.find_element(By.CSS_SELECTOR, "#detail")
        # 检查一下用户基本信息的昵称是否存在
        self.driver.find_element(By.CSS_SELECTOR, "body > div.container > div.left > div > h3")
        # 检查一下用户基本信息的文章是否存在
        self.driver.find_element(By.CSS_SELECTOR,"body > div.container > div.left > div > div:nth-child(4) > span:nth-child(1)")
        # 检查一下用户基本信息的分类是否存在
        self.driver.find_element(By.CSS_SELECTOR,"body > div.container > div.left > div > div:nth-child(4) > span:nth-child(2)")
        # 检查一下编辑按钮是否存在
        self.driver.find_element(By.CSS_SELECTOR,"body > div.container > div.right > div > div.operating > button:nth-child(1)")
        # 检查一下删除按钮是否存在
        self.driver.find_element(By.CSS_SELECTOR,"body > div.container > div.right > div > div.operating > button:nth-child(2)")
        # 添加屏幕截图
        BlogDriver.GetScreeShot()
        time.sleep(2)